package cn.com.jinhao.provider.business.service.impl;

import cn.com.jinhao.hotline.commons.base.PageCommon;
import cn.com.jinhao.hotline.commons.base.constant.SystemConstant;
import cn.com.jinhao.hotline.commons.exception.BusinessErrorException;
import cn.com.jinhao.provider.business.entity.Dispatch;
import cn.com.jinhao.provider.business.entity.RecordInfo;
import cn.com.jinhao.provider.business.entity.WorkOrderCanceled;
import cn.com.jinhao.provider.business.mapper.CancelWorkOrderMapper;
import cn.com.jinhao.provider.business.mapper.DispatchMapper;
import cn.com.jinhao.provider.business.mapper.RecordInfoMapper;
import cn.com.jinhao.provider.business.service.CancelWorkOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * hotline-spring-cloud
 * <p>Description: 撤单服务实现类</p>
 *
 * @author ShaneMew
 * @version 1.0
 * @date 2020/4/8
 * @since 2020-04-08
 */
@Service
public class CancelWorkOrderServiceImpl extends CommonService implements CancelWorkOrderService, PageCommon {
    private CancelWorkOrderMapper cancelWorkOrderMapper;
    private DispatchMapper dispatchMapper;
    private RecordInfoMapper recordInfoMapper;

    @Autowired
    public CancelWorkOrderServiceImpl(CancelWorkOrderMapper cancelWorkOrderMapper, DispatchMapper dispatchMapper) {
        this.cancelWorkOrderMapper = cancelWorkOrderMapper;
        this.dispatchMapper = dispatchMapper;
    }
    @Override
    public List<WorkOrderCanceled> queryAll(String deptCode, int pageNo, int pageSize) {
        Example example = new Example(WorkOrderCanceled.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("auditDeptCode",deptCode);
        return cancelWorkOrderMapper.selectByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(WorkOrderCanceled workOrderCanceled) throws Exception {
        // 生成唯一的CODE
        String code = this.getCode(SystemConstant.CodeType.COMPLEX);
        //TODO: 发起撤单之前需要检查该工单是否已经存在反馈

        // 通过派单编码查询到该工单的派单部门，并转变为撤单的审核部门
        Example example = new Example(Dispatch.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", workOrderCanceled.getDispatchCode());
        Dispatch dispatch = Optional.ofNullable(dispatchMapper.selectOneByExample(example)).orElseThrow(() -> new BusinessErrorException("该工单查询不到派单信息"));
        workOrderCanceled.setAuditDeptCode(dispatch.getStDeptCodeA());
        workOrderCanceled.setAuditDept(dispatch.getStDeptDeptA());
        workOrderCanceled.setCode(code);
        // 生成数据快照
        String evrv = super.getDetailMsg(workOrderCanceled.getSupplier(),workOrderCanceled.getDispatchCode(),-1,null);
        workOrderCanceled.setEvrv(evrv);
        cancelWorkOrderMapper.insertSelective(workOrderCanceled);

        // 更新派单表状态
        dispatch.setIsCancel(Short.parseShort("1"));
        dispatchMapper.updateByPrimaryKey(dispatch);

        // 更新事件表状态
        RecordInfo recordInfo = new RecordInfo();
        recordInfo.setCode(dispatch.getEventInformationCode());
//        recordInfo.setis
    }

    @Override
    public void audit(WorkOrderCanceled workOrderCanceled) throws Exception {
        // 通过撤单编码查询撤单信息
        Example example = new Example(WorkOrderCanceled.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", workOrderCanceled.getCode());
        WorkOrderCanceled orderInfo = Optional.ofNullable(cancelWorkOrderMapper.selectOneByExample(example)).orElseThrow(() -> new BusinessErrorException("该撤单信息不存在"));
        String evrv = super.getDetailMsg(workOrderCanceled.getAuditor(),orderInfo.getDispatchCode(),Integer.parseInt(workOrderCanceled.getAuditResult()),orderInfo.getSupplierCode());
        workOrderCanceled.setEvrv(evrv);
        this.update(workOrderCanceled);
    }

    @Override
    public void update(WorkOrderCanceled workOrderCanceled) {
        Example example = new Example(WorkOrderCanceled.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", workOrderCanceled.getCode());
        cancelWorkOrderMapper.updateByExampleSelective(workOrderCanceled, example);
    }

    @Override
    public int queryHistoryCount(String dispatchCode) {
        return cancelWorkOrderMapper.countCancelHistory(dispatchCode);
    }

    @Override
    public WorkOrderCanceled detail(String code) throws Exception {
        Example example = new Example(WorkOrderCanceled.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", code);
        return Optional.ofNullable(cancelWorkOrderMapper.selectOneByExample(example)).orElseThrow(() -> new BusinessErrorException("撤单信息不存在!"));
    }

    @Override
    public Map<String, Object> queryHistory(String dispatchCode, int pageNo, int pageSize) {
        Example example = new Example(WorkOrderCanceled.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("dispatchCode",dispatchCode);
        return this.getResult(example, pageNo, pageSize);
    }

    @Override
    public Map<String, Object> listAuditByCancel(String deptCode, int pageNo, int pageSize) {
        Example example = new Example(WorkOrderCanceled.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("auditDeptCode",deptCode);
        criteria.andEqualTo("auditResult", "-1");
        return this.getResult(example, pageNo, pageSize);
    }

    @Override
    public Map<String, Object> getResult(Example example, int pageNo, int pageSize) {
        HashMap<String, Object> map = new HashMap<>(10);
        if (pageNo != 0 && pageSize != 0) {
            PageHelper.startPage(pageNo, pageSize);
            PageInfo<WorkOrderCanceled> pageInfo = new PageInfo<>(cancelWorkOrderMapper.selectByExample(example));
            map.put("list", pageInfo);
            map.put("total", pageInfo.getTotal());
            map.put("pageSize", pageInfo.getPageSize());
            map.put("pageNo", pageInfo.getPageNum());
        } else {
            List<WorkOrderCanceled> dispatches = cancelWorkOrderMapper.selectByExample(example);
            map.put("list", dispatches);
            map.put("total", dispatches.size());
        }
        return map;
    }
}
